package com.mcr.reggie.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mcr.reggie.dao.ShoppingCartMapper;
import com.mcr.reggie.entity.ShoppingCart;
import com.mcr.reggie.service.ShoppingCartService;
import com.mcr.reggie.utils.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public ShoppingCart addShoppingCart(ShoppingCart sc) {
        /*原来的数据库进行购物车操作*/
//        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
//        sc.setUserId(BaseContext.getUser());
//        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getUser())
//                .eq(sc.getDishId()!=null,ShoppingCart::getDishId,sc.getDishId())
//                .eq(sc.getSetmealId()!=null,ShoppingCart::getSetmealId,sc.getSetmealId());
//        ShoppingCart shoppingCart = shoppingCartMapper.selectOne(queryWrapper);
//        if(shoppingCart!=null){
//            shoppingCart.setNumber(shoppingCart.getNumber()+1);
//            shoppingCartMapper.updateById(shoppingCart);
//        }else {
//            sc.setNumber(1);
//            sc.setCreateTime(LocalDateTime.now());
//            shoppingCartMapper.insert(sc);
//            shoppingCart=sc;
//        }
        /*优化后：用redis做购物车操作*/
        List<ShoppingCart> shoppingCartList= (List<ShoppingCart>) redisTemplate.opsForValue().get("ShoppingCart:"+BaseContext.getUser());
        if(shoppingCartList==null){
            shoppingCartList=new ArrayList<>();
        }
        boolean t=false;
        for(int i=0;i<shoppingCartList.size();i++){
            if(shoppingCartList.get(i).getUserId().longValue()==BaseContext.getUser().longValue()&&(
                (sc.getDishId()!=null&&shoppingCartList.get(i).getDishId()!=null&&shoppingCartList.get(i).getDishId().longValue()== sc.getDishId().longValue())
                        ||
                (sc.getSetmealId()!=null&&shoppingCartList.get(i).getSetmealId()!=null&&shoppingCartList.get(i).getSetmealId().longValue()==sc.getSetmealId().longValue()))
            ){
                shoppingCartList.get(i).setNumber(shoppingCartList.get(i).getNumber()+1);
                sc=shoppingCartList.get(i);
                t=true;
                break;
            }
        }
        if(!t){
            sc.setNumber(1);
            sc.setUserId(BaseContext.getUser());
            sc.setCreateTime(LocalDateTime.now());
            shoppingCartList.add(sc);
        }
        redisTemplate.opsForValue().set("ShoppingCart:"+BaseContext.getUser(),shoppingCartList,30,TimeUnit.MINUTES);
        return sc;
    }
    @Override
    public void subShoppingCart(ShoppingCart sc) {
        /*原来的数据库进行购物车操作*/
//        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
//        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getUser())
//                .eq(sc.getSetmealId()!=null,ShoppingCart::getSetmealId,sc.getSetmealId())
//                .eq(sc.getDishId()!=null,ShoppingCart::getDishId,sc.getDishId());
//        ShoppingCart shoppingCart = shoppingCartMapper.selectOne(queryWrapper);
//        Integer number=shoppingCart.getNumber();
//        if(number==1){
//            shoppingCartMapper.delete(queryWrapper);
//        }else {
//            shoppingCart.setNumber(number-1);
//            shoppingCartMapper.updateById(shoppingCart);
//        }
        /*优化后：用redis做购物车操作*/
        List<ShoppingCart> shoppingCartList= (List<ShoppingCart>) redisTemplate.opsForValue().get("ShoppingCart:"+BaseContext.getUser());
        for(int i=0;i<shoppingCartList.size();i++){
            if(shoppingCartList.get(i).getUserId().longValue()==BaseContext.getUser().longValue()&&(
                    (sc.getDishId()!=null&&shoppingCartList.get(i).getDishId()!=null&&shoppingCartList.get(i).getDishId().longValue()== sc.getDishId().longValue())
                            ||
                    (sc.getSetmealId()!=null&&shoppingCartList.get(i).getSetmealId()!=null&&shoppingCartList.get(i).getSetmealId().longValue()==sc.getSetmealId().longValue()))
            ){
                if(shoppingCartList.get(i).getNumber()==1){
                    shoppingCartList.remove(i);
                }else {
                    shoppingCartList.get(i).setNumber(shoppingCartList.get(i).getNumber()-1);
                }
                break;
            }
        }
        redisTemplate.opsForValue().set("ShoppingCart:"+BaseContext.getUser(),shoppingCartList,30,TimeUnit.MINUTES);
    }
    @Override
    public List<ShoppingCart> getList() {
        /*原来的数据库进行购物车操作*/
//        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
//        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getUser())
//                    .orderByDesc(ShoppingCart::getCreateTime);
//        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectList(queryWrapper);
        /*优化后：用redis做购物车操作*/
        List<ShoppingCart> shoppingCartList= (List<ShoppingCart>) redisTemplate.opsForValue().get("ShoppingCart:"+BaseContext.getUser());
        if(shoppingCartList==null){
            shoppingCartList=new ArrayList<>();
        }
        return shoppingCartList;
    }

    @Override
    public void cleanShoppingCart() {
        /*原来的数据库进行购物车操作*/
//        Long userId = BaseContext.getUser();
//        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
//        queryWrapper.eq(ShoppingCart::getUserId,userId);
//        shoppingCartMapper.delete(queryWrapper);
        /*优化后：用redis做购物车操作*/
        redisTemplate.delete("ShoppingCart:"+BaseContext.getUser());
    }
}
